<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="description" content="A fully featured admin theme which can be used to build CRM, CMS, etc.">
    <meta name="author" content="Coderthemes">

    <link rel="stylesheet" href="/adminstatic/base/base.css"/>
    <script src="/adminstatic/base/baseCss.js"></script>

    <style type="text/css">
        html, body {
            background: #fff;
        }

        .layui-input-block {
            margin-left: 0;
        }
    </style>
</head>

<body class="fixed-left">

<!-- Begin page -->
<div id="wrapper" style="overflow: auto;">

    <div class="container">

        <div class="panel panel-default" style="box-shadow: none">
            <div class="panel-body">
                <form class="layui-form" action="">
                    <input type="hidden" name="id" value="<%=product.id%>"/>
                    <input type="hidden" name="parent" value="<%=product.parent_id%>"/>
                    <div id="inputForm" class="form-horizontal">
                        <div class="row" style="padding:0 20px">
                            <div class="form-group">
                                <div class="col-md-12">
                                    <div class="col-xs-6 col-md-4">
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <label> <small class="notNullFlag">*</small>选择分类:</label>
                                                <select id="parent" name="parent" lay-verify="required"
                                                        lay-filter="parent">
                                                    <% for(var i=0;i
                                                    <parent.length
                                                            ;i++){ %>
                                                        <option value="<%=parent[i].id%>"><%=parent[i].title%></option>
                                                        <% } %>
                                                </select>
                                            </div>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>


                        <div class="form-group">
                            <div class="col-md-12">
                                <label for="title"><small class="notNullFlag">*</small> 产品名称</label>
                                <input class="form-control" type="text" id="title" name="title"
                                       value="<%=product.title%>" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-12">
                                <label for="series_name"><small class="notNullFlag">*</small> 产品简介</label>
                                <input class="form-control" type="text" id="series_name" name="series_name"
                                       value="<%=product.series_name%>" required>
                            </div>
                        </div>


<!--                        <div class="form-group">-->
<!--                            <div class="col-md-12">-->
<!--                                <label for="url"><small class="notNullFlag">*</small> 产品链接</label>-->
<!--                                <input class="form-control" type="text" id="url" name="url"-->
<!--                                       value="<%=product.url%>" required>-->
<!--                            </div>-->
<!--                        </div>-->

                        <div class="form-group">
                            <div class="col-md-12">
                                <div class="col-xs-6 col-md-4">
                                    <div class="layui-form-item">
                                        <label style="width: 100%;"><small class="notNullFlag">*</small> 产品图片(418×284)</label>
                                        <div class="layui-input-inline layui-btn-container"
                                             style="width: auto;float: unset;">
                                            <button type="button" class="layui-btn layui-btn-primary"
                                                    id="LAY_avatarUpload">
                                                <i class="layui-icon">&#xe67c;</i>上传图片
                                            </button>
                                        </div>
                                    </div>
                                    <div class="layui-input-inline product" id="avatar-preview">
                                        <input type="hidden" name="pic" value="<%=product.pic%>"/>
                                        <div class="img-item">
                                            <img style="width: 100%;height: 100%;" src="" onclick="showimg(this)">
                                            <div class="delete-item" onclick="deleteImg(this)">
                                                <i class="layui-icon layui-icon-close-fill"></i>
                                            </div>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>



                        <div class="form-group">
                            <div class="col-md-12">
                                <div class="col-xs-6 col-md-4">
                                    <div class="layui-form-item">
                                        <label style="width: 100%;"><small class="notNullFlag">*</small>
                                            详情图</label>
                                        <div class="layui-input-inline layui-btn-container"
                                             style="width: auto;float: unset;">
                                            <button type="button" class="layui-btn layui-btn-primary"
                                                    id="LAY_detailsUpload">
                                                <i class="layui-icon">&#xe67c;</i>上传图片
                                            </button>
                                        </div>
                                    </div>
                                    <div class="layui-input-inline details" id="avatar-preview">
                                        <input type="hidden" name="imgList" value="<%=product.img_list%>"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div id="actions">
                <button type="button" class="layui-btn layui-btn-normal" id="submitBtn">保存</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="closeIframe()">取消</button>
            </div>
        </div><!-- panel-body -->
    </div> <!-- panel -->

</div> <!-- container -->

</div>
<!-- END wrapper -->

<script src="/adminstatic/base/base.js"></script>
<script type="text/javascript">
    var pic = $("input[name='pic']").val();
    var parentValue = $("input[name='parent']").val();
    var imgList = $("input[name='imgList']").val();
    if (imgList) {
        var imglistStr = JSON.parse(imgList)
        $.each(imglistStr, function (index, item) {
            var img = $('<div class="img-item">' +
                '<img style="width: 100%;height: 100%;" src="' + item + '" onclick="showimg(this)"> ' +
                '<div class="delete-item" onclick="deleteImg(this)"> ' +
                '<i class="layui-icon layui-icon-close-fill"></i> ' +
                '</div> ' +
                '</div>');
            $(".details").append(img);
        })
    }
    $(".product .img-item img").attr("src", pic)
    var parentId;
    var form;
    layui.use('form', function () {
        form = layui.form;
        $("#parent").find("option[value='" + parentValue + "']").attr("selected", "true");
        parentId = $("#parent option:selected").val();
        // console.log(parentId)
        form.render('select');
        form.on('select(parent)', function (data) {
            //1:拿到select对象：
            parentId = $('#parent').find('option:selected').val();

            form.render('select');
        });

    });

    uploadImg('#LAY_avatarUpload', '/admin/upload/product/addFile', '#avatar-preview')
    uploadImgList('#LAY_detailsUpload', '/admin/upload/product/addFile', '.details')


    function deleteImg(that) {
        var $this = $(that);
        var name = $this[0].dataset.name;
        $this[0].parentNode.remove();
        layer.msg("删除成功", {icon: 1});
    }

    $("#submitBtn").click(function () {
        console.log(parentId);
        var imgList = [];//小程序详情图
        $(".details .img-item img").each(function () {
            imgList.push($(this).attr('src'));
        });
        var pic = $(".product .img-item img").attr('src');//产品图片
        var id = $("input[name='id']").val();//产品id
        var title = $("input[name='title']").val();//产品标题
        var seriesName = $("input[name='series_name']").val();//产品分类
        console.log("产品图：" + pic)
        if (!vaildeParam(title)) {
            layer.msg("产品名称不能为空", {icon: 5});
            return;
        }
        if (!vaildeParam(seriesName)) {
            layer.msg("产品简介不能为空", {icon: 5});
            return;
        }


        // if (!vaildeParam(url)) {
        //     layer.msg("产品链接不能为空", {icon: 5});
        //     return;
        // }

        if (!vaildeParam(pic)) {
            layer.msg("请选择产品图片", {icon: 5});
            return;
        }

        if (!vaildeParam(imgList)) {
            layer.msg("请选择产品详情图", {icon: 5});
            return;
        }


        var api = '/admin/product/update';
        var data = {
            "id": id,
            "title": title,
            "imgList":  JSON.stringify(imgList),
            "seriesName": seriesName,
            "pic": pic,
            "parentId": parentId,
        }

        postJSON(api, data, function (res) {
            cleanSuccess(res)
        })

    })
</script>
</body>
</html>